package com.delio.client.util;

import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.google.gwt.event.dom.client.KeyCodes;

public class Utilities {

	public static Integer itemsPerPage = 50;

	public static boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	public static boolean hasValue(NumberField field) {
		return field.getValue() != null
				&& field.getValue().toString().length() > 0;
	}

	public static String toIds(List<BeanModel> list) {
		String ids = "";
		for (BeanModel item : list) {
			ids += "," + item.get("id");
		}
		ids = ids.substring(1);
		return ids;
	}

	public static String toIds2(List<ModelData> list) {
		String ids = "";
		for (ModelData item : list) {
			ids += "," + item.get("id");
		}
		ids = ids.substring(1);
		return ids;
	}

	public static void setLengh(final TextField<String> txtNumeber,
			final TextField<String> txtField, final Integer lengh) {
		txtNumeber.addKeyListener(new KeyListener() {
			public void componentKeyDown(ComponentEvent event) {
				if (txtNumeber.getCursorPos() == lengh)
					txtField.focus();
			}

		});

	}

	public static void setLengh(final NumberField txtNumeber,
			final TextArea txtArea, final Integer lengh) {
		txtNumeber.addKeyListener(new KeyListener() {
			public void componentKeyDown(ComponentEvent event) {
				if (txtNumeber.getCursorPos() == lengh)
					txtArea.focus();
			}

		});

	}

	// /if (txtNumeber.getCursorPos() ==
	// lengh||event.getKeyCode()==KeyCodes.KEY_ENTER)
	public static Double tipoCambio(String cadena) {
		Double res = 0.0;
		char[] array = cadena.toCharArray();

		String numero1 = "";
		String numero2 = "";
		int aux = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == '*')
				aux = i;
			if (array[i] == ',')
				array[i] = '.';
		}

		for (int i = 0; i < array.length; i++) {
			if (i < aux)
				numero1 += array[i];
			if (i > aux)
				numero2 += array[i];
		}

		if (aux != 0) {
			Double n1 = Double.valueOf(numero1);
			Double n2 = Double.valueOf(numero2);
			res = n1 * n2;
		}

		return res;

	}

	public static void setNumerosDouble(final TextField<String> txtField) {

		txtField.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if ((c > 31 && (c < 48 || c > 57) && c != 46 && c != 42))
					event.preventDefault();

			}

		});

	}

	public static void setNumerosEnteros(final TextField<String> txtField) {

		txtField.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if (c > 31 && (c < 48 || c > 57))
					event.preventDefault();

			}

		});

	}

	public static Date getFecha(Date fecha, Integer mes) {
		Date aux_fecha = new Date();

		Integer year = fecha.getYear();
		Integer month = fecha.getMonth();
		Integer day = fecha.getDay();

		Integer aux_year = year + (month + mes) / 12;
		Integer aux_month = (month + mes) % 12;
		Integer aux_day;

		if (diaMes(aux_month, aux_year) <= day)
			aux_day = day;
		else {
			aux_day = Math.abs(diaMes(aux_month, aux_year) - day);

		}

		aux_fecha.setYear(aux_year);
		aux_fecha.setMonth(aux_month);
		aux_fecha.setDate(aux_day);

		return aux_fecha;
	}

	private static Integer diasFebrero(Integer year) {
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			return 29;
		else
			return 28;
	}

	private static Integer diaMes(Integer mes, Integer year) {
		Integer dia = 0;
		if (mes == 2)
			dia = diasFebrero(year);

		else {
			switch (mes) {
			case 1:
				dia = 31;
				break;
			case 3:
				dia = 31;
				break;
			case 4:
				dia = 30;
				break;
			case 5:
				dia = 31;
				break;
			case 6:
				dia = 30;
				break;
			case 7:
				dia = 31;
				break;
			case 8:
				dia = 31;
				break;
			case 9:
				dia = 30;
				break;
			case 10:
				dia = 31;
				break;
			case 11:
				dia = 30;
				break;
			case 12:
				dia = 31;
				break;
			}
		}

		return dia;
	}

	public static void validarEmail(TextField<String> txtEmail) {
		final String emailReg = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
		final String errMsg = "Not a valid email address!";

		txtEmail.setValidator(new Validator() {
			public String validate(Field<?> field, String value) {
				if (!value.toUpperCase().matches(emailReg))
					return errMsg;
				return null;
			}
		});

	}

	public static Integer getHour(String time) {

		char[] c = time.toCharArray();

		String suma = "";
		suma += String.valueOf(c[0]) + String.valueOf(c[1]);

		return Integer.parseInt(suma);
	}

	public static Integer getMinute(String time) {

		char[] c = time.toCharArray();
		String suma = "";
		suma += String.valueOf(c[3]) + String.valueOf(c[4]);

		return Integer.parseInt(suma);
	}

	public static void setDigitoshora(final TextField<String> txtField) {
		txtField.addKeyListener(new KeyListener() {

			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if ((c > 31 && (c < 48 || c > 57) && c != 58 && c != 42))
					event.preventDefault();

			}

			public void componentKeyDown(ComponentEvent event) {

				if (txtField.getCursorPos() == 5)
					if (event.getKeyCode() != KeyCodes.KEY_BACKSPACE)
						event.preventDefault();
			}
		});

	}

	public static Boolean validarHora(TextField<String> txtHora) {

		char ar[] = txtHora.getValue().toCharArray();
		String v0[] = new String[5];
		Integer v1;
		String v2;
		Integer v3;
		boolean valor = false;

		if (ar.length == 5) {
			for (int i = 0; i < 5; i++)
				v0[i] = String.valueOf(ar[i]);

			v1 = Integer.valueOf(v0[0] + v0[1]);
			v2 = v0[2];
			v3 = Integer.valueOf(v0[3] + v0[4]);

			if (v1 < 24 && v2.equals(":") && v3 < 60)
				valor = true;
			else
				valor = false;

		} else
			valor = false;

		if (ar.length == 8)
			valor = true;

		return valor;
	}

	public static Label getLabel(String title, String pos) {
		Label label = new Label(title);
		label.setStyleAttribute("marginLeft", pos);

		label.setStyleAttribute("fontSize", "13px");
		label.setStyleAttribute("fontWeight", "bold");
		label.addStyleName("x-panel-header-text-default");

		return label;
	}

	public static void focoEnter(final Component comp, final Component comp2) {

		comp.addListener(Events.KeyDown, new KeyListener() {

			public void componentKeyDown(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c == KeyCodes.KEY_ENTER)
					comp2.focus();

			}

		});

	}
	
	public static void Enter(Component A[]){
		  for(int i=0;i<A.length-1;i++)
			 focoEnter(A[i], A[i+1]);
		  
	}
	
	@SuppressWarnings("unchecked")
	public static void centrarTexto(final Component comp){
		
		((Field<Number>) comp).setInputStyleAttribute("textAlign","center");
		
	}

}
